﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;

namespace SCP
{

    /// <summary>
    /// <para>All Posible Commands, which can be sent to the service</para>
    /// <para>unkown: if no valid command was sent</para>
    /// <para>ReloadSettings: read Settings <example>Example: Interval</example></para>
    /// <para>Search: start a new Search</para>
    /// <para>ChangeConfigFile: Read the new Path for the ConfigFile</para>
    /// <para>Pause: Pauses the Service</para>
    /// <para>Continue: Continues the Service</para>
    /// <para>Stop: Permanently Stops the Service</para>
    /// </summary>
    enum Command { unknown = 127, ReloadSettings, Search, ChangeConfigFile}

    class SimpleCommandProgram
    {

        /// <summary>
        /// <para>Main Program</para>
        /// <para>When Started sends Custom Command to InvIT service</para>
        /// </summary>
        /// <param name="args">
        /// <para>args[0]: name of the service, Example: <example>"InvIT"</example></para>
        /// <para>args[1]: Custom Command, Example: <example>"-r"</example></para>
        /// </param>
        static void Main(string[] args)
        {
            if (args.Length == 2)
            {
                try
                {
                    ServiceController sc = new ServiceController(args[0]); //Initialize new Instance of a Service Controller, Target is the given Service Name
                    Command com=Command.unknown;    //Command which should be sent
                    bool startCommand = true;
                    switch (args[1]) //switch input
                    {
                        case "-r":
                            com = Command.ReloadSettings;
                            break;
                        case "-s":
                            com = Command.Search;
                            break;
                        case "-c":
                            com = Command.ChangeConfigFile;
                            break;
                        case "-p":
                            sc.Pause();
                            startCommand = false;
                            break;
                        case "-co":
                            sc.Continue();
                            startCommand = false;
                            break;
                        case "-stop":
                            sc.Stop();
                            startCommand = false;
                            break;
                        case "-start":
                            sc.Start();
                            startCommand = false;
                            break;
                        default:
                            com = Command.unknown;  //if invalid command was recognized
                            break;
                    }
                    if (startCommand)
                    {
                        if (com != Command.unknown) //if valid command was given, send Command
                        {
                            sc.ExecuteCommand((int)com);    //Cast Enum to int and sent it to the service
                            WriteSuccess();
                        }
                        else
                            WriteError();
                    }
                    else
                        WriteSuccess();
                }
                catch
                {
                    WriteError();
                }
            }
            else
                WriteError();
        }

        static void WriteError()
        {
            Console.WriteLine("Error");
        }

        static void WriteSuccess()
        {
            Console.WriteLine("Success");
        }
    }
}
